import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useBMIStore = defineStore('bmi', () => {
    const bmiList = ref([])

    const fetchBMIList = async () => {
        // 将await的返回值保存成一个变量
        const res = await fetch('http://120.27.209.174:1010/api/v1/bmi').then(res => {
            return res.json()
        })
        bmiList.value = res
    }

    const createBMI = async (val) => {
        const res = await fetch('http://120.27.209.174:1010/api/v1/bmi', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(val.value)
        }).then(res => res.json())
        console.log(res)
        // await fetchBMIList()
    }

    const deleteBMI = async () => {
        await fetch(`http://120.27.209.174:1010/api/v1/bmi/${id}`, {
            method: 'DELETE'
        }).then(res => res.json())
    }

    return { bmiList, fetchBMIList, createBMI, deleteBMI }
})